package com.service.healthycode.controller;

import com.service.config.annotation.Distinct;
import com.service.config.utils.*;
import com.service.healthycode.dto.InfoDeclareDto;
import com.service.healthycode.dto.SmsCode;
import com.service.healthycode.dto.UserDto;
import com.service.healthycode.entity.InfoDeclare;
import com.service.healthycode.service.InfoDeclareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;

/**
 * @author: hjl
 * @date: 2022/05/12 18:25
 * @description:
 */
@CrossOrigin
@RestController()
@RequestMapping("/infoDeclare")
@Api(value = "信息申报 ",tags = "信息申报接口")
public class InfoDeclareController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private InfoDeclareService infoDeclareService;

    /**
     * 信息申报
     * @param
     * @return
     */
    @PostMapping(value = "infoDeclare",produces="application/json")
    @ApiOperation(value = "信息申报")
    @ResponseBody
    public Result infoDeclare(@RequestBody InfoDeclareDto infoDeclareDto,
                              @RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                return infoDeclareService.addInfoDeclare(infoDeclareDto,userId);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }


    /**
     * 更新信息申报
     * @param
     * @return
     */
    @PostMapping(value = "updateInfoDeclare",produces="application/json")
    @ApiOperation(value = "更新信息申报")
    @ResponseBody
    public Result updateInfoDeclare(@RequestBody InfoDeclareDto infoDeclareDto,
                              @RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                return infoDeclareService.updateInfoDeclare(infoDeclareDto,userDto.getId());
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 查询用户申报信息
     * @param
     * @return
     */
    @RequestMapping(value =  "queryInfoDeclare",method = RequestMethod.GET,produces="application/json")
    @ApiOperation(value = "查询用户申报信息")
    @ResponseBody
    public Result queryInfoDeclare(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                InfoDeclare infoDeclare = infoDeclareService.queryInfoDeclare(userId);
                return Result.ok().setData(infoDeclare);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 查询用户申报信息是否过期
     * @param
     * @return
     */
    @RequestMapping(value =  "queryInfoExpired",method = RequestMethod.GET,produces="application/json")
    @ApiOperation(value = "查询用户申报信息是否过期")
    @ResponseBody
    public Result queryInfoExpired(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                return infoDeclareService.queryInfoExpired(userDto.getTelephone());
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 用户根据手机号发送短信验证码
     * @param
     * @return
     */
    @PostMapping(value = "SendSms",produces="application/json")
    @ApiOperation(value = "用户根据手机号发送短信验证码")
    @ResponseBody
    @Distinct
    public Result SendSms(@RequestHeader(value = "cloud-Auth-Token",required = true) String cloudAuthToken,
                          @RequestBody SmsCode smsCode) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (StringUtils.isEmpty(smsCode.getTelephone())) {
                    return Result.failure(DATA_FORMAT_WRONG,INCORRECT_DATA_FORMAT);
                }
                if (RegexUtil.isMobileSimple(smsCode.getTelephone())) {
                    return infoDeclareService.sendSms(smsCode);
                }
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }else {
                return Result.failure(10004,"登录信息已失效");
            }
        }else {
            return Result.failure(USER_NOT_LOGIN,"用户未登录");
        }
    }

    /**
     * 查询用户申报信息是否过期
     * @param
     * @return
     */
    @RequestMapping(value =  "queryStatus",method = RequestMethod.GET,produces="application/json")
    @ApiOperation(value = "查询用户健康码状态")
    @ResponseBody
    public Result queryStatus(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);

                return infoDeclareService.queryStatus(userDto.getTerminalMac(),userId);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
